<template>
  <div style="width: 100%; height: 100%; min-width: 700px;">
    <div
      :class="{ mask_notice: isChange }"
      @click="isChange=false;currentHospital=lastHospital"
    />
    <div class="notice_header">
      <span class="notice_header_title">
        <span style="border-left: 5px solid #21a6e6;">&nbsp;&nbsp;</span>
        <i class="el-icon-s-home"/>
        首页
      </span>
      <el-link
        type="primary"
        class="notice_header_change"
        :underline="false"
      >
        <span v-if="!isChange" @click="isChange=true;lastHospital=currentHospital">更改</span>
        <span v-else @click="handleHospitalChange">确定</span>
      </el-link>
      <div v-if="!isChange">
        <div class="notice_header_info">
          <span style="font-weight: bold;">当前检测点：&nbsp;</span>
          <span>{{ currentHospital }}</span>
        </div>
      </div>
      <div v-else>
        <el-select
          class="notice_header_select"
          v-model="currentHospital"
          filterable
          placeholder="请选择检测点"
        >
          <el-option
            v-for="item in hospitals"
            :key="item.hospital_id"
            :label="item.name"
            :value="item.name"
          >
            <span style="float: left; margin-right: 20px;">{{ item.name }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.address }}</span>
          </el-option>
        </el-select>
      </div>
    </div>
    <div class="notice_main">
      <div class="notice_pic" v-loading="loading">
        <el-carousel
          trigger="click"
          height="400px"
        >
          <el-carousel-item v-for="item in noticeArray" :key="item.src">
            <img class="notice_pic_picture" :src="item.src" @click="checkNotice(item)">
            <span class="notice_pic_title" @click="checkNotice(item)">{{ item.title }}</span>
          </el-carousel-item>
        </el-carousel>
      </div>
      <div class="notice_title" v-loading="loading">
        <div class="notice_title_header">通知公告</div>
        <div v-if="defaultNotice.length === 0">
          <el-empty
            style="margin-top: 20px;"
            description="这个医院还没有发布过公告哦~"
          />
        </div>
        <div style="height: 570px;" v-else>
          <div
            class="notice_title_info"
            v-for="item in defaultNotice.slice((currentPage - 1) * pageSize, currentPage * pageSize)"
            :key="item.title"
          >
            <span
              class="notice_title_info_content"
              @click="checkNotice(item)"
            >
              {{ item.title }}
            </span>
            <span class="notice_title_info_time">
              <i class="el-icon-time"/>
              {{ item.time }}
            </span>
          </div>
        </div>
        <el-pagination
          style="text-align: center;"
          @current-change="handlePageChange"
          :current-page="currentPage"
          :page-size="pageSize"
          :total="defaultNotice.length"
          layout="prev, pager, next"
          hide-on-single-page
        />
      </div>
    </div>
  </div>
</template>

<script>
import {
  userHospitalNotices,
  userHospitalRegions
} from '@/api/result-service';
export default {
  data() {
    return {
      isChange: false,
      noticeArray: [],
      defaultNotice: [],
      hospitals: [],
      lastHospital: "",
      currentHospital: "",
      currentPage: 1, // 当前页数
      pageSize: 8, // 设置分页数
      loading: true
    }
  },
  created() {
    userHospitalRegions().then((data) => {
      this.hospitals = data.sort((a, b) => {
        return a.hospital_id - b.hospital_id;
      });
      this.currentHospital = this.hospitals[0].name;
      this.initNotice(this.hospitals[0]);
    });
  },
  methods: {
    initNotice(hospital) {
      this.loading = true;
      userHospitalNotices(hospital).then((data) => {
        this.defaultNotice = data.sort((a, b) => {
          if (a.time < b.time) return 1;
          else return -1;
        });
        this.noticeArray.length = 0;
        this.noticeArray[0] = {};
        this.noticeArray[0].src = require('@/assets/notice/image1.png');
        for (let i = 0 ; i < Math.min(4, this.defaultNotice.length); ++i) {
          const item = this.defaultNotice[i];
          item.src = require(`@/assets/notice/image${i+1}.png`);
          this.noticeArray[i] = item;
        }
        this.loading = false;
      })
    },
    handleHospitalChange() {
      const input = this.hospitals.filter((item) => {return item.name === this.currentHospital})[0];
      if (!input || !input.name || input.name === this.lastHospital) {
        this.isChange = false;
      } else {
        this.initNotice(input);
        this.isChange = false;
      }
    },
    handlePageChange(val) {
      this.currentPage = val;
    },
    checkNotice(val) {
      if (this.defaultNotice.length === 0) return;
      val.hospitalName = this.currentHospital;
      this.$store.commit('home/setNotice', val);
      this.$router.push('noticeDetail');
    }
  }
}
</script>

<style lang="scss">
.mask_notice {
  position: fixed;
  z-index: 49;
  width: 100%;
  height: calc(100% - 51px);
  top: 111px;
  background-color: rgba(0, 0, 0, 0.3);
}

.notice_header {
  background: #FFFFFF;
  border-bottom: 1px solid #DCDFE6;
  width: calc(100% - 60px);
  height: 50px;
  margin: 0px 30px;
  position: relative;
  z-index: 50;
  
  .notice_header_title {
    float: left;
    font-weight: bold;
    font-size: 22px;
    line-height: 50px;
    margin-left: 5px;
  }

  .notice_header_change {
    float: right;
    margin-right: 20px;
    line-height: 50px;
    font-size: 16px;
  }

  .notice_header_select {
    width: 50%;
    float: right;
    margin-right: 40px;
    line-height: 50px;
  }

  .notice_header_info {
    float: right;
    line-height: 50px;
    margin-right: 40px;
  }
}

.notice_main {
  
  .notice_pic {
    float: right;
    width: calc(100% - 700px);
    margin: 49px 49px 49px 49px;
    border: 1px solid #8C8C8C;

    .notice_pic_picture {
      height: 370px;
      width: 100%;
    }

    .notice_pic_title {
      cursor: pointer;
      height: 30px;
      font-size: 20px;
      color: #FFFFFF;
      padding: 0px 0px 0px 5px;
    }
  }

  .notice_title {
    cursor: pointer;
    float: left;
    height: 700px;
    width: 500px;
    background: #FFFFFF;
    margin: 50px 50px 0px 50px;
    border-radius: 10px;
    box-shadow: 1px 1px 2px 2px rgba(0,0,0,0.2);

    .notice_title_header {
      width: 180px;
      padding-top: 20px;
      margin-left: 20px;
      font-size: 26px;
      font-weight: bold;
      color: #005187;
      border-bottom: 3px solid #005187;
    }

    .notice_title_info {
      background: #F3F3F3;
      height: 50px;
      width: 90%;
      margin: 20px auto 0px auto;
      border-radius: 5px;

      .notice_title_info_content {
        color: #176CC4;
        display: block;
        text-overflow: ellipsis;
        overflow-x: hidden;
        font-size: 18px;
        padding-left: 5px;
      }

      .notice_title_info_time {
        float: right;
        font-size: 14px;
        color: #8C8C8C;
        padding: 5px 5px 0px 0px;
      }
    }
  }
}

.el-carousel__item {
  background-color: #99a9bf;
}
</style>

<style lang="scss" scoped>
@media screen and (max-width: 1350px){
  .notice_pic {
    float: none;
    width: calc(min(100% - 100px, 600px));
    margin: 50px auto 50px auto;
  }

  .notice_title {
    float: none;
    margin: 0px calc((100% - 500px) / 2) 50px calc((100% - 500px) / 2);
  }
}

</style>
